package com.kaichaohulian.baocms.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.adapter.HelpSongDetailAdapter;
import com.kaichaohulian.baocms.app.MyApplication;
import com.kaichaohulian.baocms.base.BaseMyActivity;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.newmodel.HelpQuDetailModel;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.view.ShowDialog;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpAlreadyPaylActivity extends BaseMyActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.exit_layout)
    LinearLayout exitLayout;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.center_title_tv)
    TextView centerTitleTv;
    @BindView(R.id.tv_right_text)
    TextView tvRightText;
    @BindView(R.id.navigation_bar)
    RelativeLayout navigationBar;
    @BindView(R.id.img_purchase)
    ImageView imgPurchase;
    @BindView(R.id.img_ji)
    ImageView imgJi;
    @BindView(R.id.text_title)
    TextView textTitle;

    @BindView(R.id.text_time)
    TextView textTime;
    @BindView(R.id.text_static)
    TextView textStatic;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.text_money)
    TextView textMoney;

    @BindView(R.id.text_nickname)
    TextView textNickname;

    @BindView(R.id.text_address)
    TextView textAddress;
    @BindView(R.id.edit_shuoming)
    TextView editShuoming;
    String type = "1";
    @BindView(R.id.img_status)
    ImageView img_status;


    //取消发布按钮
    @BindView(R.id.textcancel_fabu)
    TextView textcancel_fabu;



    @BindView(R.id.recycle_pic)
    RecyclerView recycle_pic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_already_fukuan);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
        if (type.equals("1")) {
            imgPurchase.setImageResource(R.mipmap.mai);
        } else if (type.equals("2")) {
            imgPurchase.setImageResource(R.mipmap.qu);
        } else {
            imgPurchase.setImageResource(R.mipmap.song);
        }
        recycle_pic.setLayoutManager(new GridLayoutManager(HelpAlreadyPaylActivity.this, 3));
        getDetailData();


    }

    List<String> list = new ArrayList<>();
    HelpSongDetailAdapter helpSongDetailAdapter;

    public void getDetailData() {

        final RequestParams params = new RequestParams();
        params.put("userId", MyApplication.getInstance().UserInfo.getUserId());
        params.put("demandId", getIntent().getStringExtra("demandId"));
        HttpUtil.post(Url.XuqiuDetail, params, new JsonHttpResponseHandler() {
            @Override
            public void onStart() {
                showLoading();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response.optString("code").equals("1")) {
                    Toast.makeText(HelpAlreadyPaylActivity.this, "分类不存在", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Log.d(response.toString(), "333333333333333");
                    Gson gson = new Gson();
                    HelpQuDetailModel helpQuDetailModel = new HelpQuDetailModel();
                    helpQuDetailModel = gson.fromJson(response.toString(), HelpQuDetailModel.class);
                    textTitle.setText(helpQuDetailModel.getDataObject().getTitle());
//                    if (helpQuDetailModel.getDataObject().getStatusDesc().equals("已付款")) {
                    list = helpQuDetailModel.getDataObject().getPictures();
                    img_status.setImageResource(R.mipmap.yifukuan);



                    helpSongDetailAdapter = new HelpSongDetailAdapter(list);
                    recycle_pic.setAdapter(helpSongDetailAdapter);

                    helpSongDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                            ImagePagerActivity.startImagePagerActivity(HelpAlreadyPaylActivity.this, list, position, imageSize);


                        }
                    });

                    textTime.setText(helpQuDetailModel.getDataObject().getCreatedTime());
                    textStatic.setText("关键字：" + helpQuDetailModel.getDataObject().getKeyWord());
                    textMoney.setText(helpQuDetailModel.getDataObject().getReward());
                    textNickname.setText(helpQuDetailModel.getDataObject().getUserName());


//                商品地址

                    textAddress.setText(helpQuDetailModel.getDataObject().getReceiverAddress());
                    //相关说明
                    editShuoming.setText("相关说明:" + helpQuDetailModel.getDataObject().getRemark());
                    dismissLoading();
                }

            }

            @Override
            public void onFinish() {
//                dismissLoading();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                dismissLoading();
            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });

    }

    @OnClick({R.id.iv_back, R.id.tv_title, R.id.center_title_tv, R.id.tv_right_text, R.id.navigation_bar, R.id.img_purchase, R.id.img_ji, R.id.text_title, R.id.text_time, R.id.text_static, R.id.btn_submit, R.id.text_money, R.id.text_nickname, R.id.text_address, R.id.edit_shuoming, R.id.textcancel_fabu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title:
                break;
            case R.id.center_title_tv:
                break;
            case R.id.tv_right_text:
                break;
            case R.id.navigation_bar:
                break;
            case R.id.img_purchase:
                break;
            case R.id.img_ji:
                break;
            case R.id.text_title:
                break;

            case R.id.text_time:
                break;
            case R.id.text_static:
                break;
            case R.id.btn_submit:
                break;
            case R.id.text_money:
                break;

            case R.id.text_nickname:
                break;

            case R.id.text_address:
                break;
            case R.id.edit_shuoming:
                break;
            //取消发布
            case R.id.textcancel_fabu:
                getplatFormData();
                break;

        }
    }

    //取消发布
    public void getplatFormData() {


        final RequestParams params = new RequestParams();
        params.put("userId", MyApplication.getInstance().UserInfo.getUserId());
        params.put("demandId", getIntent().getStringExtra("demandId"));
        HttpUtil.post(Url.Cancel_do, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                if (response.optString("code").equals("0")) {
                    Toast.makeText(HelpAlreadyPaylActivity.this, "取消发布成功", Toast.LENGTH_SHORT).show();
                    finish();

                } else {
                    Toast.makeText(HelpAlreadyPaylActivity.this, response.optString("errorDescription"), Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();

            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });

    }


}
